#include "alarm.h"

Alarm::Alarm()
{     
#ifdef Q_WS_WIN
    file = path = QCoreApplication::applicationDirPath();
#else 
    path = QDir::homePath ()+"/.qalarm";
    imgfile = "/usr/share/qalarm/";
#endif    
    init();
    createActions();
    createTrayIcon();
    trayIcon = new QSystemTrayIcon(this);
    playFile = new QProcess(this);
    url = new QUrl();    
    updateTrayIcon();
    //music = Phonon::createPlayer(Phonon::MusicCategory,Phonon::MediaSource(utils.readFile(path+"/data/audio")));
} 
 
void Alarm::init()
{        
    //timer = startTimer(1000);
    //QTimerEvent * e = new QTimerEvent(timer);
    QCoreApplication::postEvent(this,e);    
}
 
void Alarm::createActions()
{
    setalarm = new QAction(tr("Set ala&rm"), this);
    connect(setalarm, SIGNAL(triggered()), this, SLOT(setAlarm()));

    setaudio = new QAction(tr("Set Au&dio"), this);
    connect(setaudio, SIGNAL(triggered()), this, SLOT(setAudio()));
    
    playaudio = new QAction(tr("&Play Audio"), this);
    connect(playaudio, SIGNAL(triggered()), this, SLOT(playAudio()));
    
    stopaudio = new QAction(tr("S&top Audio"), this);
    connect(stopaudio, SIGNAL(triggered()), this, SLOT(stopAudio()));
    
    seturl = new QAction(tr("Set U&rl"), this);
    connect(seturl, SIGNAL(triggered()), this, SLOT(setUrl()));
    
    openurl = new QAction(tr("Op&en Url"), this);
    connect(openurl, SIGNAL(triggered()), this, SLOT(openUrl()));
    
    aboutAction = new QAction(tr("&About"), this);
    connect( aboutAction, SIGNAL(triggered()), this, SLOT(_about()));
    
    aboutQtAction = new QAction(tr("About Qt"), this);
    connect( aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

    quitAction = new QAction(tr("&Quit"), this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));   
}

void Alarm::createTrayIcon()
{    
    QColor *c = new QColor ( 238, 238, 157, 255 );    
    QPalette * p = new QPalette();
    p->setColor(QPalette::ToolTipBase,*c);
    QFont *f = new QFont ( "sans", 10, -1, false );    
    QToolTip::setFont ( *f );
    QToolTip::setPalette(*p);    
    trayIconMenu = new QMenu(this);        
    trayIconMenu->addAction(setalarm);
    trayIconMenu->addAction(setaudio);
    trayIconMenu->addAction(playaudio);
    trayIconMenu->addAction(stopaudio);
    trayIconMenu->addAction(seturl);
    trayIconMenu->addAction(openurl);    
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(aboutAction);
    trayIconMenu->addAction(aboutQtAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction); 
    stopaudio->setEnabled(false);  
    playaudio->setEnabled(true);          
}

void Alarm::setAlarm()
{
    //Alarm * alarmAlarm = new Alarm();
    //alarmAlarm.show();
}

void Alarm::setAudio()
{
    //Audio * audioAlarm = new Audio();
    //audioAlarm.show();
}

void Alarm::playAudio()
{    
    //music->setCurrentSource(utils.readFile(path+"/data/audio"));
    //stopaudio->setEnabled(true);   
    //playaudio->setEnabled(false);        
    //music->play();        
}

void Alarm::stopAudio()
{  
    //~ music->stop();
    //~ stopaudio->setEnabled(false);   
    //~ playaudio->setEnabled(true);    
}

void Alarm::setUrl()
{
   //Url * urlAlarm = new Url();
   //urlAlarm.show();
}

void Alarm::openUrl()
{           
    //~ Utils utils;
    //~ url->setUrl(utils.readFile(path+"/data/url"));
    //~ QDesktopServices::openUrl(*url);
}

void Alarm::startAlarm()
{
    //~ openUrl();
    //~ playAudio();
}

void Alarm::updateTrayIcon()
{
    //~ Utils utils;
    //~ repeat = getRepeat();    
    //~ QString tooltipmsg1 = utils.readFile(path+"/data/audio");    
    //~ QStringList list = tooltipmsg1.split("/");
    //~ tooltipmsg1 = list[list.size()-1];
    //~ QString tooltipmsg2 = utils.readFile(path+"/data/alarm");    
    //~ QString tooltipmsg3 = utils.readFile(path+"/data/url");
    //~ QString tooltipmsg4;        
    //~ if (repeat == 1){        
        //~ QString list[7] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
           //~ tooltipmsg4 = "Repeat once a week \n Day = "+list[utils.readFile(path+"/data/day").toInt()];        
    //~ }
    //~ else if (repeat == 2){
        //~ 
          //~ tooltipmsg4 = "Repeat week days";
    //~ }    
          //~ if (repeat == 3){        
           //~ tooltipmsg4 = "Repeat every days";        
    //~ }
    //~ else if (repeat == 0 || repeat == -1){             
        //~ tooltipmsg4 = "Repeat once \n Date = "+utils.readFile(path+"/data/date");
    //~ }
    //QString message = " Alarm = "+tooltipmsg2+"\n Repeat = "+tooltipmsg4+"\n Audio = "+tooltipmsg1+"\n Url = "+tooltipmsg3;
    QString message = "Message test";
    trayIcon->setIcon(QIcon(imgfile+"/images/alarm.png"));    
    trayIcon->setToolTip(message);
    trayIcon->show();    
    trayIcon->setContextMenu(trayIconMenu);    
}

void Alarm::_about()
{;
    QString cp = "©";
    QMessageBox::about(this, tr("About Alarm"),
             "<b> Alarm application V0.3.1</b> Copyright " +  QString::fromUtf8(cp.toLatin1().data()) + " 2008 Skander Jabouzi skander@skanderjabouzi.com<br>"
                 " This is a free software distributed under the terms of the GNU General Public License version 3\n(http://www.gnu.org/licenses/gpl-3.0.html)");     
}

int Alarm::getRepeat()
{
    //~ Utils utils;
    //~ int radio = utils.readFile(path+"/data/repeat").toInt();    
    //~ return radio;
    return 0;
}
//
 
